package com.bdqn.util;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.entity.util.RespBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HttpUtils {
    private static Logger logger = LoggerFactory.getLogger(HttpUtils.class);

    /**
     * 根据返回对象将数据写会前端
     * @param response
     * @param respBean
     */
    public static void outputJson(HttpServletResponse response,  RespBean respBean) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.append(JSONObject.toJSON(respBean).toString());
        } catch (IOException e) {
            logger.error("ResponseOut-->error:{}", e.getMessage());
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
